﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectRoughWork
{
    class ProjectGameLogic : TestGameLogic
    {
        public enum GameState { MainMenu, Playing, Paused, Results, GameOver };

        public ProjectGameLogic() : base()
        {
            scMng = ScreenManager.GetInstance();

            MainMenuScreen mainMenu = new MainMenuScreen();
            scMng.AddScreen(mainMenu);

            state = GameState.MainMenu;

            InputListener inputListener = new InputListener();
            StateChangeListener stateListener = new StateChangeListener();
            ScreenChangeListener screenChangeListener = new ScreenChangeListener();

            //Adds input listener to event manager
            //Note: Might extract this to it's own method for setting up listeners at a later date

            evtMng.VAddListener("KeyPressed", inputListener);
            evtMng.VAddListener("MouseClicked", inputListener);
            evtMng.VAddListener("ChangeGameState", stateListener);
            evtMng.VAddListener("ChangeScreen", screenChangeListener);
        }

        public static IGameLogic GetInstance()
        {
            if (pInstance == null)
            {
                pInstance = new ProjectGameLogic();
            }

            return pInstance;
        }

        public void ChangeState(GameState newState)
        {
            if (newState == GameState.Playing)
            {

                if (state == GameState.MainMenu)
                {
                    SquadActor[] squad = ((SquadSelectionScreen)ScreenManager.GetInstance().getTop()).PlayerSquad;
                    GameplayScreen gameScreen = new GameplayScreen(squad);
                    ActorManagementListener acMngListener = new ActorManagementListener(gameScreen);

                    //Add an assortment of listeners to handle events for the gameplay screen

                    evtMng.VAddListener("AddActor", acMngListener);
                    evtMng.VAddListener("RemoveActor", acMngListener);
                    evtMng.VAddListener("MoveActor", acMngListener);
                    evtMng.VAddListener("AttachProcess", acMngListener);
                    evtMng.VAddListener("DealDamage", acMngListener);
                    evtMng.VAddListener("GiveXP", acMngListener);
                    evtMng.VAddListener("CoverCheck", acMngListener);
                    evtMng.VAddListener("Explode", acMngListener);

                    //Pop the top screen and add the new one

                    scMng.popScreen();
                    scMng.AddScreen(gameScreen);
                }
                //Change the state to the new state

                state = newState;
            }
            else if (newState == GameState.Results)
            {
                Squad squad = ((GameplayScreen)ScreenManager.GetInstance().getTop()).PlayerSquad;
                ResultScreen rsScreen = new ResultScreen(squad);

                //Remove previous listeners

                scMng.popScreen();
                scMng.AddScreen(rsScreen);

                state = newState;
            }
        }

        /// <summary>
        /// A method for changing the screen. 
        /// </summary>
        /// <param name="newScreen">The new screen to be switched to</param>

        public void ChangeScreen(IScreen newScreen)
        {
            scMng.popScreen();
            scMng.AddScreen(newScreen);
        }

        /// <summary>
        /// Method to test if the game is currently in the Playing state
        /// </summary>
        /// <returns></returns>

        public bool isPlaying()
        {
            return (state == GameState.Playing);
        }

        public override void VOnUpdate()
        {
            base.VOnUpdate();

            scMng.Update();


        }

        public override void VRender()
        {
            base.VRender();

            scMng.Draw();
        }

        //Data Memebers

        IScreenManager scMng;
        GameState state;

    }
}
